<?php


namespace app\admin\controller;


use think\Controller;
use think\Image;
use think\Db;
class Photo extends Controller
{
    public function index(){
        $admin=session('admin');
        if(empty($admin)){
            $this->error('您未登录，不能进去后台管理','/admin/index/login');
        }
        $goodsres=db('goods')->field(['id','goods_name'])->paginate(6);
        $this->assign('goodsres',$goodsres);
        $this->assign('numleft',1);
        return view();
    }

    public function lst($id){
        $admin=session('admin');
        if(empty($admin)){
            $this->error('您未登录，不能进去后台管理','/admin/index/login');
        }
        $goodsphoto=db('photo')->field(['id','og_photo'])->where('goods_id',$id)->select();
        $this->assign(['goodsphoto'=>$goodsphoto,
                        'goodsid'=>$id,
            ]);
            $this->assign('numleft',1);
        return view('list');
    }
    public function del($id){
        $admin=session('admin');
        if(empty($admin)){
            $this->error('您未登录，不能进去后台管理','/admin/index/login');
        }
        $photo=[];
        $delphoto=db('photo')->where('id',$id)->select();
        foreach ($delphoto as $value){
            $photo[]='../public/static/admin/uploads/'.$value['og_photo'];
            $photo[]='../public/static/admin/uploads/'.$value['sm_photo'];
            $photo[]='../public/static/admin/uploads/'.$value['mid_photo'];
            $photo[]='../public/static/admin/uploads/'.$value['big_photo'];
        }

        foreach ($photo as $k=>$v){
            @unlink($v);
        }
        db('photo')->delete($id);
        // dump(Db::name('photo')->getLastsql());
        // die;
        $this->success('成功删除相册','/admin/photo/lst?id='.input('goods_id'));
    }


    public function add(){
        $admin=session('admin');
        if(empty($admin)){
            $this->error('您未登录，不能进去后台管理','/admin/index/login');
        }
        if(request()->isPost()){
               $photoName=input('post.thumb');
                $ogphoto = date("Ymd") . '/' . $photoName;
                $bigphoto = date("Ymd") . '/' . 'big_' . $photoName;
                $midphoto = date("Ymd") . '/' . 'mid_' . $photoName;
                $smphoto = date("Ymd") . '/' . 'sm_' . $photoName;
                $image = Image::open('../public/static/admin/uploads/' . $ogphoto);
                $image->thumb(880, 880)->save('../public/static/admin/uploads/' . $bigphoto);
                $image->thumb(430, 430)->save('../public/static/admin/uploads/' . $midphoto);
                $image->thumb(40, 40)->save('../public/static/admin/uploads/' . $smphoto);
                db('photo')->insert(['goods_id' => input('post.goods_id'), 'big_photo' => $bigphoto,'og_photo'=>$ogphoto, 'mid_photo' => $midphoto, 'sm_photo' => $smphoto]);
                unset($info);
                $this->success('添加相册成功','lst?id='.input('post.goods_id'));
        }
        $this->assign(['goodsid'=>input('get.id')]);
        $this->assign('numleft',1);
        return view();
    }
    public function uploads()//上传模块
    {
        $admin=session('admin');
        if(empty($admin)){
            $this->error('您未登录，不能进去后台管理','/admin/index/login');
        }
        $file = request()->file('file');

        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move('../public/static/admin/uploads');
        if(!$info){
            $this->error($file->getError());
        }else{
            $file_a= $info->getFilename();
            echo '{"code":0,"msg":"成功上传","url":"'.$file_a.'","date":"'.date("Ymd").'"}';
        }
        unset($info);
    }
}